package com.chat.service;

import com.chat.FileAddress;
import com.chat.io.ChangeImage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileService {
    public void download(byte[] data,int count,String username){
        String address= FileAddress.CLIENT_CACHE+"path"+username+"_"+count;
        try {
            File file=new File(address);
            FileOutputStream fos=new FileOutputStream(file);
            fos.write(data,0,data.length);
            System.out.println("加载分片文件"+count);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void merger(String address,int size,String username){
        String add=FileAddress.LOCAL_WARHOUSE+address;
        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream=new FileOutputStream(add,true);
            File file=null;
            for(int i=1;i<=size;i++){
                file=new File(FileAddress.CLIENT_CACHE+"path"+username+"_"+i);
                byte[] bytes=new ChangeImage().get(file);
                fileOutputStream.write(bytes);
            }
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
